package com.loan.psychicloan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.ItemClickSupport;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.Util.RecycleViewDivider;
import com.loan.psychicloan.adapter.LoanAdapter;
import com.loan.psychicloan.bean.HomeBean;
import com.loan.psychicloan.bean.LoanListBean;
import com.loan.psychicloan.fragment.FragmentLoanPage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by watilion on 2017/12/29 0029.
 */

public class BtnActivity extends AppCompatActivity {

    private String mURL;
    private long loanid;
    private RecyclerView mRecyclerView;
    private List<LoanListBean.DataBean> mBtnLoanlist = new ArrayList <>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_btn);
        Toolbar mToolbar = findViewById(R.id.back_toolbar);
        TextView tv_back_toolbar_title = findViewById(R.id.back_toolbar_title);
        TextView tv_notice_text = findViewById(R.id.tv_notice_text);
        mRecyclerView = findViewById(R.id.btn_recylerview);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        //Bundle bundle = this.getIntent().getExtras();
        String URL = "https://www.psychicloan.com:8443/loan-api/loanlist/getloanlist?type=";
        //mURL = URL + bundle.get("btnid").toString();
        mURL = URL + getIntent().getStringExtra("btnid");
        tv_back_toolbar_title.setText(getIntent().getStringExtra("btnname"));
        tv_notice_text.setText(getIntent().getStringExtra("btn_notice"));

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL));

        //设置item的点击事件监听
        ItemClickSupport mItemClickSupport = ItemClickSupport.addTo(mRecyclerView);
        mItemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, View itemView, int position) {
                Intent mIntent = new Intent(getApplicationContext(), LoanInfoActivity.class);
                mIntent.putExtra("loanid",mBtnLoanlist.get(position).loanid);

                startActivity(mIntent);
            }
        });
        //开启网络下载数据的方法
        startTask();

    }

    private void startTask() {

        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().getBeanOfOk(this, mURL, LoanListBean.class,
                new OkHttpUtils.CallBack <LoanListBean>() {
                    @Override
                    public void getData(LoanListBean loanListBean) {
                        if (loanListBean != null){
                            //如果不为空就用mLoanList接收
                            mBtnLoanlist.addAll(loanListBean.data);

                            initAdapter();
                        }
                    }
                });
    }
    private void initAdapter() {

        //创建自定义适配器对象
        LoanAdapter loanAdapter = new LoanAdapter(this, mBtnLoanlist);

        //设置recyclerview适配器
        mRecyclerView.setAdapter(loanAdapter);

        //刷新适配器
        loanAdapter.notifyDataSetChanged();
    }

}
